package com.hoshiicloud.user.controller.rbac;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.UsersLoginVO;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.rbac.dto.UserPermissionDTO;
import com.hoshiicloud.rbac.entity.SysUserRole;
import com.hoshiicloud.rbac.entity.SysUsers;
import com.hoshiicloud.rbac.vo.SysUserRoleVO;
import com.hoshiicloud.rbac.vo.SysUsersDetailVO;
import com.hoshiicloud.rbac.vo.SysUsersVO;
import com.hoshiicloud.user.param.rbac.SysUsersInsertParam;
import com.hoshiicloud.user.param.rbac.SysUsersQueryParam;
import com.hoshiicloud.user.param.rbac.SysUsersUpdateParam;
import com.hoshiicloud.user.service.rbac.ISysUserRoleService;
import com.hoshiicloud.user.service.rbac.ISysUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统用户控制器
 *
 * @author Mr.wu
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("/rbac/sys-users")
public class SysUsersController {

    @Autowired
    private ISysUsersService sysUsersService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @ApiOperation(value = "增加系统用户拥有的角色")
    public Response addSysUserHoldRoles() {
        return null;
    }

    @ApiOperation(value = "根据用户名和密码获取系统用户信息", notes = "返回值包括用户的角色与权限")
    @RequestMapping(value = "/getLoginDetail", method = RequestMethod.POST)
    public Response<TokenUsersVO> getSysUserByPassword(@RequestBody @Valid @NotBlank(message = "用户名不能为空") String username) {
        return Response.data(sysUsersService.getLoginDetail(username));
    }

    @ApiOperation("新增系统用户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response addSysUser(@Valid @RequestBody SysUsersInsertParam param) {
        SysUsers user = new SysUsers();
        BeanUtils.copyProperties(param, user);
        // TODO 获取创建人
        user.setCreatedBy("admin");
        // TODO 只接受指定参数
        // 接收角色数组，并新增对应关联

        return sysUsersService.addSysUser(user, param.getRoleIdList());
    }

    @ApiOperation("修改系统用户")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Response updateSysUser(@Valid @RequestBody SysUsersUpdateParam param) {

        SysUsers entity = new SysUsers();
        BeanUtils.copyProperties(param, entity);
        // TODO 参数过滤：只接受指定参数
        entity.setModifiedTime(LocalDateTime.now());
        return sysUsersService.updateSysUser(entity, param.getRoleIdList());
    }

    @ApiOperation("删除系统用户")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response deleteSysUser(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return sysUsersService.deleteSysUser(Long.valueOf(id));
    }

    @ApiOperation("获取系统用户详情")
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    public Response<SysUsersDetailVO> getSysUserDetail(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return sysUsersService.getSysUserDetail(Long.valueOf(id));
    }

    @ApiOperation("获取系统用户列表")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public Response<List<SysUsersVO>> getSysUserList(@Valid @RequestBody SysUsersQueryParam param) {

        return sysUsersService.getSysUserList(param.convertToQueryWrapper());
    }

    @ApiOperation("获取系统用户所拥有的角色")
    @RequestMapping(value = "getRoleList", method = RequestMethod.GET)
    public Response<List<SysUserRoleVO>> getSysUserRoleList(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        SysUserRole param = new SysUserRole();
        param.setUserId(Long.valueOf(id));
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<SysUserRole>(param);
        return sysUserRoleService.getSysUserRoleList(queryWrapper);

    }

    @ApiOperation(value = "获取用户所拥有的权限", notes = "返回权限值列表")
    @RequestMapping(value = "getUserPermission", method = RequestMethod.GET)
    public Response<List<String>> getSysUserPermissionList(@RequestParam String id) {
        UserPermissionDTO userPermissionDTO = sysUsersService.getSysUserPermissionList(Long.valueOf(id)).getData();
        return Response.data(userPermissionDTO.getPermissionValueList());
    }

    @ApiOperation("分页查询系统用户")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public Response<IPage<SysUsersVO>> getSysUserPage(@Valid @RequestBody SysUsersQueryParam param) {
        QueryWrapper<SysUsers> queryWrapper = param.convertToQueryWrapper();
        IPage<SysUsers> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
        return sysUsersService.getPage(page, queryWrapper,key);
    }

}
